package com.beamfield.controller.manage.device;

import com.alibaba.fastjson.JSON;
import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.*;
import com.beamfield.modelVo.DeviceVo;
import com.beamfield.service.manage.device.DeviceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Set;

/**
 * 设备采购管理
 */
@Controller
@RequestMapping("/cms/device/purchase")
public class DeviceController {


    @Value("${session.beam}")
    private String beam;
    @Value("${session.user}")
    private String user;

    @Autowired
    private DeviceService deviceService;
    @RequestMapping(value = "/purchase_list", method = RequestMethod.GET)
    public ModelAndView purchase() {
        return new ModelAndView();
    }
    @RequestMapping(value = "/addPurchase", method = RequestMethod.GET)
    public ModelAndView addPurchase() {
        return new ModelAndView();
    }
    @RequestMapping(value = "/purchaseDetails", method = RequestMethod.GET)
    public ModelAndView details() {
        return new ModelAndView();
    }
    /**
     * 获取采购计划
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/readPurchaseData", method = RequestMethod.GET)
    @ResponseBody
    public PageResult purchaseList(HttpServletRequest request) {
        String projectId = request.getParameter("projectId");
        String name = request.getParameter("name");
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String type=request.getParameter("type");
        String demandid=request.getParameter("demandid");
        String recordSource=request.getParameter("recordSource");
        long beamId = (long) request.getSession().getAttribute(this.beam);
        int inttype=0;
        if(StringUtils.isNotBlank(type)) {
            inttype = Integer.parseInt(type);
        }
        List<DevPurch> purchase = deviceService.getPurchase(projectId, name, inttype, offset, limit, demandid, beamId, recordSource);
        long total = deviceService.countPurchase(projectId, name, inttype,demandid, beamId, recordSource);
        return new PageResult(total, Long.parseLong(offset), purchase);
    }

    /**
     * 获取全部采购计划
     *
     * @return
     */
    @RequestMapping(value = "/readAllPurchaseData", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult purchaseList(HttpSession session,String type,String recordSource) {
        ApiResult apiResult=new ApiResult();
        long beamId = (long) session.getAttribute(this.beam);
        apiResult.setResult(deviceService.getAllPurchase(beamId,type,recordSource));
        return apiResult;
    }
    @RequestMapping(value = "/getDeviceDemandNameList", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getDeviceDemandNameList(HttpServletRequest request) {
        ApiResult apiResult=new ApiResult();
        long beamId = (long) request.getSession().getAttribute(this.beam);
        String recordSource = request.getParameter("recordSource");
        List<DeviceDeman> all = deviceService.getDeviceDemandNameList(beamId, recordSource);
        apiResult.setResult(all);
        return apiResult;
    }

    /**
     * 获取全部采购计划名称
     *
     * @return
     */
    @RequestMapping(value = "/readAllDeviceName", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult readAllDeviceName(HttpSession session,long device_purchase_id) {
        ApiResult apiResult=new ApiResult();
        long beamId = (long) session.getAttribute(this.beam);
        Set<String> strings = deviceService.readAllDeviceName(beamId, device_purchase_id);
        apiResult.setResult(strings);
        return apiResult;
    }

    /**
     * 获取设备规格
     * @param device_name
     * @param device_purchase_id
     * @return
     */
    @RequestMapping(value = "/readDevice_standard", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult readDevice_standard(String  device_name,long device_purchase_id) {
        ApiResult apiResult=new ApiResult();
        Set<String> strings = deviceService.readDevice_standard(device_name, device_purchase_id);
        apiResult.setResult(strings);
        return apiResult;
    }
    /**
     * 新增采购计划
     *
     * @param purchaseDetailJson 采购明细列表
     * @param name       采购计划名称
     * @param session
     * @return
     */
    @RequestMapping(value = "/doSave", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSave(String purchaseDetailJson,String deviceDetailJson, String name,long device_demand_id,  HttpSession session) {
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        long beamId = (long) session.getAttribute(this.beam);
        long userId = (long) session.getAttribute(this.user);
        if (StringUtils.isEmpty(purchaseDetailJson)) {
            apiResult.setResult(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage(HttpResultEnum.MIS_PARAM.getMessage());
            return apiResult;
        }
        if (StringUtils.isEmpty(name)) {
            apiResult.setResult(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage("请输入采购计划名称");
            return apiResult;
        }
        try {
            List<DevPurchDetail> purchaseDetailList = JSON.parseArray(purchaseDetailJson, DevPurchDetail.class);
            List<Device> deviceList = JSON.parseArray(deviceDetailJson, Device.class);
            if (purchaseDetailList == null || purchaseDetailList.isEmpty()||deviceList==null||deviceList.isEmpty()) {
                apiResult.setResult(HttpResultEnum.MIS_PARAM.getCode());
                apiResult.setMessage("采购计划申请不能为空");
                return apiResult;
            }
            apiResult = deviceService.save(purchaseDetailList, name, 1, beamId, userId,deviceList,device_demand_id);
        } catch (Exception e) {
            e.printStackTrace();
            apiResult.setCode(HttpResultEnum.EXCEPTION.getCode());
            apiResult.setMessage(HttpResultEnum.EXCEPTION.getMessage());
        }
        return apiResult;
    }

    /**
     * 获取采购计划明细列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/readPurchaseDetailsData", method = RequestMethod.GET)
    @ResponseBody
    public PageResult purchaseDetailsList(HttpServletRequest request) {
        String purchaseId = request.getParameter("purchaseId");
        if(StringUtils.isEmpty(purchaseId)){
            return null;
        }
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String name = request.getParameter("name");
        List<DeviceVo> purchaseDetails = deviceService.getPurchaseDetails(purchaseId, name, offset, limit);
        long total = deviceService.countPurchaseDetails(purchaseId, name);
        return new PageResult(total, Long.parseLong(offset), purchaseDetails);
    }

    @RequestMapping("/saveSituation")
    @ResponseBody
    public ApiResult saveSituation(DeviceSituation deviceSituation, HttpServletRequest request){
        if(deviceSituation == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM);
        }
        long beamId = (long) request.getSession().getAttribute(this.beam);
        long userId = (long) request.getSession().getAttribute(this.user);
        return deviceService.saveSituation(deviceSituation, beamId, userId);
    }

    @RequestMapping(value = "/readSituation", method = RequestMethod.GET)
    @ResponseBody
    public PageResult readSituation(HttpServletRequest request) {
        String inboundId = request.getParameter("inboundId");
        if(StringUtils.isEmpty(inboundId)){
            return null;
        }
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        return deviceService.readSituation(inboundId, offset, limit);
    }
}
